package leetcode.二维数组.dfs;

public class Leetcode695 {

    int[][] towards = {{1,0},{0,1},{-1,0},{0,-1}};
    int cols;
    int rows;
    int max = 0;
    int ans;
    public int maxAreaOfIsland(int[][] grid) {
        rows = grid.length;
        cols = grid[0].length;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                ans = 0;
                if(grid[i][j]==1){
                    dfs(i,j,grid);
                }
                max = Math.max(ans,max);
            }
        }
        return max;
    }


    public void dfs(int x,int y,int[][] grid){
        ans++;
        grid[x][y] = 0;
        for (int i = 0; i < 4; i++) {
            int newX = x+towards[i][0];
            int newY = y+towards[i][1];
            if(isArea(newX,newY)&&grid[newX][newY]==1){
                dfs(newX,newY,grid);
            }
        }
    }

    public boolean isArea(int x,int y){
        if(x>=0&&x<rows&&y>=0&&y<cols){
            return true;
        }
        return false;
    }
}
